/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import Model.*;
import java.sql.SQLException;

/**
 *
 * @author Master
 */
public class controller_TaiKhoan {
    boolean temp = false;
    String quyen = "";
    
    public List<model_TaiKhoan> LoadList() throws SQLException {
        DataProvider data = new DataProvider();
        data.Open();

        List<model_TaiKhoan> list = new ArrayList<>();
        String query = "Select * from tai_khoan";
        ResultSet rs = data.ExecuteQuery(query);

        while (rs.next()) {
            model_TaiKhoan taiKhoan = new model_TaiKhoan();

            taiKhoan.setMATKHAU(rs.getString("MatKhau"));
            taiKhoan.setNGAYTAO(rs.getString("NgayTao"));
            taiKhoan.setMANV(rs.getString("MaNV"));
            taiKhoan.setQUYEN(rs.getString("Quyen"));
            taiKhoan.setTEMTK(rs.getString("TenTK"));

            list.add(taiKhoan);
        }
        return list;
    }

    public void Insert(model_TaiKhoan taiKhoan) {
        DataProvider data = new DataProvider();
        data.Open();

        String password = data.MD5(taiKhoan.getMATKHAU() + taiKhoan.getNGAYTAO());
        String query = String.format("INSERT INTO `tai_khoan`(`TenTK`, `MatKhau`, `MaNV`, `Quyen`, `NgayTao`) VALUES ('%s','%s','%s','%s','%s')", taiKhoan.getTEMTK(), password, taiKhoan.getMANV(), taiKhoan.getQUYEN(), taiKhoan.getNGAYTAO());

        data.ExecuteUpdate(query);
    }

    public void Update(model_TaiKhoan taiKhoan) {
        DataProvider data = new DataProvider();
        data.Open();

        String password = data.MD5(taiKhoan.getMATKHAU() + taiKhoan.getNGAYTAO());
        String query = String.format("UPDATE `tai_khoan` SET `TenTK`=TenTK,`MatKhau`= MatKhau,`MaNV`=MaNV,`Quyen`='%s',`NgayTao`=NgayTao WHERE TenTK = '%s'", taiKhoan.getQUYEN(), taiKhoan.getTEMTK());

        data.ExecuteUpdate(query);
    }

    public void Delete(model_TaiKhoan taiKhoan) {
        DataProvider data = new DataProvider();
        data.Open();

        String query = String.format("DELETE FROM `tai_khoan` WHERE tentk = '%s'", taiKhoan.getTEMTK());

        data.ExecuteUpdate(query);
    }

    public boolean DangNhap(String tentk, String matkhau) throws SQLException {
        List<model_TaiKhoan> dn = LoadList();
        DataProvider data = new DataProvider();

        for (model_TaiKhoan taiKhoan1 : dn) {
            String str = data.MD5(matkhau + taiKhoan1.getNGAYTAO());
            if (taiKhoan1.getMATKHAU().equals(str) && taiKhoan1.getMANV().equals(tentk)){
                temp = true;
                quyen = taiKhoan1.getQUYEN();
                break;
            }
        }
        return temp;
    }

    public String TenNV(String manv) throws SQLException{
        DataProvider data = new DataProvider();
        data.Open();

        String query = String.format("SELECT `MaNV`, `HoTen` FROM `nhan_vien` WHERE `MaNV` = '%s'", manv);
        ResultSet rs = data.ExecuteQuery(query);
        String tennv = "";

        while (rs.next()) {
            tennv = rs.getString("HoTen");
        }
        return tennv;
    }
}
